//
//  VCExt.swift
//  kano
//
//  Created by Sophia on 2024/8/14.
//

import UIKit


extension UIViewController {
    
    static func ka_currentNavigationController() -> UINavigationController {
        var var_CurrentVC = UIViewController.ka_current()
        var var_CurrentNav = var_CurrentVC.navigationController
        if (var_CurrentNav == nil) {
            var var_tabbarVC : UITabBarController = UIViewController.ka_rootViewController()
            if var_tabbarVC.isKind(of: UITabBarController.self) {
                var_CurrentNav = (var_tabbarVC.selectedViewController as! UINavigationController)
            } else {
                var_CurrentNav = var_CurrentVC.navigationController
            }
        }
        return var_CurrentNav!
    }
    
    static func ka_current() -> UIViewController {
        let var_rootVC = UIViewController.ka_window().rootViewController
        return getVisibleViewController(from: var_rootVC!)
        func getVisibleViewController(from vc: UIViewController) -> UIViewController {
            if let presentedVC = vc.presentedViewController {
                return getVisibleViewController(from: presentedVC)
            } else if let navigationController = vc as? UINavigationController {
                return getVisibleViewController(from: navigationController.visibleViewController ?? vc)
            } else if let tabBarController = vc as? UITabBarController {
                return getVisibleViewController(from: tabBarController.selectedViewController ?? vc)
            } else {
                return vc
            }
        }
    }

    static func ka_tabVC() -> UITabBarController? {
        if let window = UIApplication.shared.windows.first,
           let tabBarController = window.rootViewController as? UITabBarController {
            return tabBarController
        }
        return nil
    }

    @objc public static func ka_window() -> UIWindow {
        if let var_window: UIWindow = (UIApplication.shared.delegate?.window)! {
            debugPrint(var_window)
            return var_window
        } else {
            if #available(iOS 13.0, *) {
                let var_arr: Set = UIApplication.shared.connectedScenes
                let var_windowScene: UIWindowScene = var_arr.first as! UIWindowScene
                
                // SceneDelegate * delegate = (SceneDelegate *)windowScene.delegate;
                //UIWindow * mainWindow = delegate.window;
                if let var_mainWindow: UIWindow = var_windowScene.value(forKeyPath: "delegate.window") as? UIWindow {
                    return var_mainWindow
                } else {
                    return UIApplication.shared.windows.first!
                }
            }else{
                return UIApplication.shared.keyWindow!
            }
        }
    }
    
    static func ka_rootViewController() -> UITabBarController {
        let var_rootVC = UIViewController.ka_window().rootViewController
        let var_rootViewController = var_rootVC as! UITabBarController
        return var_rootViewController
    }


}


